<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;

class FinanceBillMonthAllExport implements FromArray, WithHeadings,ShouldAutoSize
{

    protected $collection;

    public function __construct($collection)
    {
        $this->collection = $collection;
    }

    //数组数据
    public function array(): array
    {
        return $this->collectionToArray($this->collection);
    }

    //表头
    public function headings(): array
    {
        return [
            '序号',
            '渠道分类',
            '渠道站点',
            '账号数量',
            '外包服务部门',
            '是否预付',
            '客户名',
            '店铺名',
            '业务员',
            '服务日期',
            '账号',
            '服务天数',
            '咨询量',
            '客服销售额',
            '客服销售退款金额',
            '客服实际接单金额',
            '日咨询量',
            '客户收费标准',
            '服务报价',
            '客户提成率',
            '服务底薪费',
            '店铺管理费/其他应收',
            '提成费',
            '加班费',
            '本期费用合计',
            '上期应收余额',
            '留抵金额',
            '拼多多平台已收金额',
            '保证金抵扣',
            '其他',
            '税额',
            '线下收款',
            '退客户金额',
            '应收余额',
            '已收保证金',
            '预付款',
            '合同日期',
            '备注',
        ];
    }

    //集合转数组
    public function collectionToArray($collection):array{
        $arr=[];
        if(count($collection)){
            foreach ($collection as $k=>$item){
                $arr[$k]['id']=$k+1;
                $arr[$k]['service_type']=$item->shop->quotaionInfo->object_name??'';
                $arr[$k]['platform_type']=$item->shop->quotaionInfo->plateform_name??'';
                $arr[$k]['subaccount_num']='';
                $arr[$k]['depart_name']=$item->subAccount->department->name??'';
                $arr[$k]['is_prepay']='';
                $arr[$k]['company_name']=$item->company_name;
                $arr[$k]['shop_name']=$item->shop_name;
                $arr[$k]['system_user_name']=$item->customer->system_user_name??'';
                $arr[$k]['service_period']=$item->service_period;
                $arr[$k]['sub_account']=$item->sub_account;
                $arr[$k]['service_days']=$item->service_days;
                $arr[$k]['consult_quantity']=$item->consult_quantity;
                $arr[$k]['sale_acount']=$item->sale_acount;
                $arr[$k]['refund_acount']=$item->refund_acount;
                $arr[$k]['actual_turnover']=$item->actual_turnover;
                $arr[$k]['quote_day_average_consult']=$item->quote_day_average_consult;
                $arr[$k]['charge_standard']= str_replace("</br>","\n",$item->charge_standard??'');
                $arr[$k]['service_quote_final']=$item->service_quote_final;
                $arr[$k]['commission_fee_rate']=$item->commission_fee_rate.'%';
                $arr[$k]['service_salary']=$item->service_quote_final;
                $arr[$k]['shop_other_fee']=$item->shop_other_fee;
                $arr[$k]['commission_fee']='';
                $arr[$k]['overtime_fee']=$item->overtime_fee;
                $arr[$k]['all_this_period_fee']=$item->all_this_period_fee;
                $arr[$k]['previous_receivable']=$item->previous_receivable;
                $arr[$k]['previous_reserved']=$item->previous_reserved;
                $arr[$k]['pdd_received']=$item->pdd_received;
                $arr[$k]['deposit_cost']=$item->deposit_cost;
                $arr[$k]['other_fee']='';
                $arr[$k]['tax_amount']='';
                $arr[$k]['offline_amount']='';
                $arr[$k]['refund_customer']='';
                $arr[$k]['customer_period_pay']=$item->customer_period_pay;
                $arr[$k]['received_deposit']='';
                $arr[$k]['prepay_amount']='';
                $arr[$k]['contract_date']='';
                $arr[$k]['sub_bill_remark']='';
            }
        }
        return $arr;
    }


}
